package jp.co.mclnet.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.TreeSet;

// 指定ディレクトリ以下の総サイズを取得
// ファイル名ソート

public class ShimanukiFile {
    public static class BothFilter implements FilenameFilter {
        private FilenameFilter filter;

        @Override
        public boolean accept(File dir, String name) {
            System.out.println("----------");
            File file = new File(dir, name);
            System.out.println(file);
            boolean accept = this.getFilter().accept(dir, name);
            boolean directory = file.isDirectory();
            System.out.println(accept);
            System.out.println(directory);
            return accept || directory;
        }

        public FilenameFilter getFilter() {
            return filter;
        }

        public void setFilter(FilenameFilter filter) {
            this.filter = filter;
        }

    }

    public class BothShimanukiFilter implements ShimanukiFilter {
        private ShimanukiFilter filter;

        @Override
        public boolean accept(File file, String s) {
            return this.getFilter().accept(file, s);
        }

        @Override
        public boolean accept2(File file, String s) {
            return true;
        }

        public ShimanukiFilter getFilter() {
            return filter;
        }

        public void setFilter(ShimanukiFilter filter) {
            this.filter = filter;
        }

    }

    public static class NoFilter implements FilenameFilter {
        @Override
        public boolean accept(File dir, String name) {
            return true;
        }
    }

    public interface ShimanukiFilter {
        abstract boolean accept(File file, String s);

        public boolean accept2(File file, String s);
    }

    public class ShimanukiFilterText implements ShimanukiFilter {

        @Override
        public boolean accept(File file, String s) {
            return false;
        }

        @Override
        public boolean accept2(File file, String s) {
            return false;
        }
    }

    public class ShimanukiFilterXls implements ShimanukiFilter {

        @Override
        public boolean accept(File file, String s) {
            return true;
        }

        @Override
        public boolean accept2(File file, String s) {
            return true;
        }
    }

    public static class TxtFi extends TxtFilter {
        @Override
        public boolean accept(File dir, String name) {
            return super.accept(dir, name) || dir.isDirectory();
        }
    }

    public static class TxtFilter implements FilenameFilter {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".txt");
        }
    }

    public static class TxtFilter9 implements FileFilter {
        @Override
        public boolean accept(File paramFile) {
            return false;
        }
    }

    static ArrayList<File> getAllFile(File dir) {
        ArrayList<File> ary = new ArrayList<File>();
        File[] list = dir.listFiles();
        for (File file : list) {
            if (file.isFile()) {
                ary.add(file);
            } else if (file.isDirectory()) {
                ary.addAll(getAllFile(file));
            } else {
                System.out.println("エラー");
            }
        }
        return ary;
    }

    static ArrayList<File> getAllFile(File dir, FilenameFilter filter) {
        ArrayList<File> ary = new ArrayList<File>();
        File[] dirList = dir.listFiles();
        for (File file : dirList) {
            if (file.isDirectory()) {
                ary.addAll(getAllFile(file));
            }
        }
        File[] fileList = dir.listFiles(filter);
        for (File file : fileList) {
            if (file.isFile()) {
                ary.add(file);
            }
        }
        return ary;
    }

    static ArrayList<File> getAllFile2(File dir) {
        FilenameFilter filter = new NoFilter();
        return getAllFile2(dir, filter);
    }

    static ArrayList<File> getAllFile2(File dir, FilenameFilter filter) {
        BothFilter both = new BothFilter();
        both.setFilter(filter);
        ArrayList<File> ary = new ArrayList<File>();
        File[] list = dir.listFiles(both);
        for (File file : list) {
            if (file.isFile()) {
                ary.add(file);
            } else if (file.isDirectory()) {
                ary.addAll(getAllFile2(file, filter));
            } else {
                System.out.println("エラー");
            }
        }
        return ary;
    }

    public static void main(String[] args) {
        File targetDir = new File("N:/users/shimanuki/書類");
        System.out.println(Arrays.toString(name(targetDir)).replaceAll(", ",
                "\n"));
        // System.out.println(size(targetDir) + "byte");
    }

    static File[] name(File dir) {
        TxtFilter txtFilter = new TxtFilter();
        ArrayList<File> aryFile = getAllFile2(dir, txtFilter);
        File[] paramArrayOfT = new File[0];
        File[] array = aryFile.toArray(paramArrayOfT);
        return array;
    }

    static int size(File dir) {
        if (!dir.exists()) {
            System.out.println("存在しないディレクトリ");
            System.exit(0);
        }
        ArrayList<File> aryFile = getAllFile(dir);
        int allSize = 0;
        for (File file : aryFile) {
            allSize = (int) (allSize + file.length());
        }
        return allSize;
    }

    static void sort(File dir) {
        if (!dir.exists()) {
            System.out.println("存在しないディレクトリ");
            System.exit(0);
        }
        Collection<File> coll = new TreeSet<File>();
        for (File file : coll) {
            System.out.println(file);
        }
    }
}
